package org.finesys.common.des.config;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.ulisesbocchio.jasyptspringboot.properties.JasyptEncryptorConfigurationProperties;

/**
 * 自定义加密配置
 */
@Configuration
@EnableConfigurationProperties(JasyptEncryptorConfigurationProperties.class)
public class DefaultEncryptor {

    @Bean
    @Primary
    StringEncryptor jasyptorStringEncryptor(JasyptEncryptorConfigurationProperties jasyptEncryptorConfigurationProperties) {
        return new StringEncryptor() {
            @Override
            public String encrypt(String val) {
                StandardPBEStringEncryptor stringEncryptor = new StandardPBEStringEncryptor();
                stringEncryptor.setPassword(jasyptEncryptorConfigurationProperties.getPassword());
                return stringEncryptor.encrypt(val);
            }

            @Override
            public String decrypt(String val) {
                StandardPBEStringEncryptor stringEncryptor = new StandardPBEStringEncryptor();
                stringEncryptor.setPassword(jasyptEncryptorConfigurationProperties.getPassword());
                return stringEncryptor.decrypt(val);
            }
        };
    }
}
